package com.wentao.mall.commons;

import com.wentao.mall.exception.MallExceptionEnum;
import lombok.ToString;

/**
 * 统一响应结果封装
 */
@ToString
public class Result<T> {
    private Integer status;
    private String msg;
    private T data;

    //响应正常状态码
    private static final Integer OK_CODE = 10000;
    //响应正常信息
    private static final String OK_MSG = "SUCCESS";

    public Result(Integer status,String msg,T data)
    {
        this.data=data;
        this.msg=msg;
        this.status=status;
    }
    public Result(Integer status,String msg)
    {
        this.msg=msg;
        this.status=status;
    }
    public Result()
    {
        this(OK_CODE,OK_MSG);
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Result success(T data)
    {
        Result<T> result=new Result();
        result.setData(data);
        return result;
    }

    //封装响应失败的结果
    public static Result error(Integer status,String msg)
    {
        return new Result(status,msg);
    }

    public static Result error(MallExceptionEnum exception){
        return new Result(exception.getStatus(),exception.getMsg());
    }
}